// import count from '@feidao/web/atom/nodejs/db/count';
import query from '@dfeidao/atom-nodejs/db/query';
// import remove_duplicate from '@feidao/web/atom/core/arr/remove-duplicate';
// import { IBaseTable } from '@feidao/web/atom/nodejs/db/interfaces';

interface Message {
	productid: string;
	client_type: string;
}

interface Data {
	formid: string;
	formname: string;
}
interface Da {
	menu_no: string;
	menu_name: string;
	parent_no: string;
	_id: string;
	formid: string;
	client_type: string;
	systemid: string;
	description: string;
	formname: string;
	priority: string;
	icon: string;
	refsystemid: string;
	background: string;
	is_show: boolean;
}

export default async function atom(msg: Message, action_id: string, session_id: string) {

	const systemid = '12';
	const { exec, prepare } = query(action_id, session_id, systemid);
	const productid = msg.productid;
	const client_type = msg.client_type;
	prepare('pub_menu', ['menu_no', 'menu_name', 'priority', 'systemid', 'parent_no', 'formid', 'refsystemid', 'client_type', 'description', 'icon', 'show_type', 'background', 'is_show'], { productid, client_type }, 100, 1, ['priority'], []);
	const [data] = await exec<Data>();
	const arr1 = new Array() as [{}];
	data.map((v) => {
		if (v.formid) {
			arr1.push(v.formid);
		}
	});
	function uniq(array: [{}]) {
		const temp = []; // 一个新的临时数组
		for (let i = 0; i < array.length; i++) {
			if (temp.indexOf(array[i]) === -1) {
				temp.push(array[i]);
			}
		}
		return temp;
	}
	const resultarr = uniq(arr1);
	prepare('pub_form', ['formid', 'formname'], { formid: { $in: resultarr }, productid, client_type }, 100, 1, [], []);
	const [data1] = await exec();
	const dataArray = data.map((item) => {
		const formid1 = item.formid;
		const ck_data = data1.filter((d: { formid: string }) => {
			return d.formid === formid1;
		});
		const arr = new Array();
		ck_data.forEach((_item: { formname: string }) => {
			const formname = _item.formname;
			arr.push(formname);
			return;
		});
		item.formname = arr[0] ? arr[0] : '';
		return item;
	});
	const cdata = dataArray.map((d: Da) => {
		// console.log('11111111111111111111');
		return { id: d.menu_no, text: d.menu_name, p_id: d.parent_no ? d.parent_no : '', _id: d._id, formid: d.formid ? d.formid : '', systemid: d.systemid, client_type: d.client_type, description: d.description ? d.description : '', priority: d.priority, formname: d.formname ? d.formname : '', icon: d.icon ? d.icon : '', refsystemid: d.refsystemid ? d.refsystemid : '', background: d.background ? d.background : '', is_show: d.is_show };
	});
	// return cdata;/
	// console.log(JSON.stringify(data), '------==========--------');
	// console.log(cdata, '--------------');
	return cdata;
}
